<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
    <!--    导入BootStrap的css文件-->
    <link rel="stylesheet" href="bootstrap3/css/bootstrap.min.css">
    <link rel="stylesheet" href="bootstrap3/css/bootoast.css">
    <!--    导入jquery-->
    <script src="bootstrap3/jquery.js" type="text/javascript" charset="utf-8"></script>
    <!--    导入bootstrap中的js文件-->
    <script src="bootstrap3/js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="bootstrap3/js/bootoast.js" type="text/javascript" charset="utf-8"></script>
    <style>
        form {
            margin-top: 120px;
        }

        .div1 {
            padding: 10px;
        }
    </style>
    <script>
        $(function () {
            //类型选择
            $.ajax({
                url: 'find_type',
                type: 'get',
                cache: false,
                dataType: 'json',
                success: function (rst) {
                    $('#bookType').empty();
                    $(`<option value="-1">请选择类型</option>`).appendTo('#bookType');
                    if (rst.code==1){
                        for (let i = 0; i < rst.data.length; i++) {
                            var t = rst.data[i];
                            $(`<option value="${t.typeId}">${t.typeName}</option>`).appendTo('#bookType');
                        }
                    }
                }
            });
            $('#btnBook').click(function () {
                if ($('#bookName').val() == '') {
                    bootoast({
                        message: '图书名称不能为空',
                        type: 'danger',
                        position: 'top',
                        timeout: 2
                    });
                } else {
                    if ($('#TypeId').val() == '') {
                        bootoast({
                            message: '请选择图书类型',
                            type: 'danger',
                            position: 'top',
                            timeout: 2
                        });
                    } else {
                        if ($('#bookWriter').val() == '') {
                            bootoast({
                                message: '图书作者不能为空',
                                type: 'danger',
                                position: 'top',
                                timeout: 2
                            });
                        } else {
                            if ($('#bookPress').val() == '') {
                                bootoast({
                                    message: '图书出版社不能为空',
                                    type: 'danger',
                                    position: 'top',
                                    timeout: 2
                                });
                            } else {
                                if ($('#bookMoney').val() == '') {
                                    bootoast({
                                        message: '图书价格不能为空',
                                        type: 'danger',
                                        position: 'top',
                                        timeout: 2
                                    });
                                }else {
                                    if ($('#bookMoney').val()<=0) {
                                        bootoast({
                                            message: '图书价格不正确',
                                            type: 'danger',
                                            position: 'top',
                                            timeout: 2
                                        });
                                    }else{
                                        if ($('#bookRepertory').val() == '') {
                                            bootoast({
                                                message: '图书库存不能为空',
                                                type: 'danger',
                                                position: 'top',
                                                timeout: 2
                                            });
                                        }else{
                                            if ($('#bookRepertory').val()<=0) {
                                                bootoast({
                                                    message: '图书库存不正确',
                                                    type: 'danger',
                                                    position: 'top',
                                                    timeout: 2
                                                });
                                            }else {
                                                $.ajax({
                                                    url: 'add_book',
                                                    type: 'post',
                                                    cache: false,
                                                    data: {
                                                        bookName:$('#bookName').val(),
                                                        typeId:$('#bookType').val(),
                                                        author:$('#bookWriter').val(),
                                                        publish:$('#bookPress').val(),
                                                        depict:$('#bookDescribe').val(),
                                                        price:$('#bookMoney').val(),
                                                        storages:$('#bookRepertory').val()
                                                    },
                                                    dataType: 'json',
                                                    success: function (rst) {
                                                        if (rst.code == -1) {
                                                            bootoast({
                                                                message: rst.msg,
                                                                type: 'danger',
                                                                position: 'top',
                                                                timeout: 2
                                                            });
                                                        } else {
                                                            bootoast({
                                                                message: rst.msg,
                                                                type: 'success',
                                                                position: 'top',
                                                                timeout: 2
                                                            });
                                                            $('#bookName').val(''),
                                                            $('#bookType').val(-1),
                                                            $('#bookWriter').val(''),
                                                            $('#bookPress').val(''),
                                                            $('#bookDescribe').val(''),
                                                            $('#bookMoney').val(''),
                                                            $('#bookRepertory').val('')
                                                        }
                                                    },
                                                });
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            });
        });
    </script>
</head>

<body>

<ul class="breadcrumb">
    <li><a href="922welcome.html">首页</a></li>
    <li><a href="#">狗熊岭图书管理</a></li>
    <li>添加图书</li>
</ul>
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-12">
            <!--            垂直表单-->
            <!--            名称,类别编号,作者,出版社,描述,价格,库存-->
            <form class="form-horizontal">
                <div class="form-group col-sm-6 div1">
                    <label for="bookName" class="col-sm-8 control-label">图书名称:</label>
                    <div class="col-sm-4 input">
                        <input type="text" id="bookName" class="form-control">
                    </div>
                </div>
                <div class="form-group col-sm-6 div1">
                    <label for="bookType" class="col-sm-4 control-label">图书类型:</label>
                    <div class="col-sm-4">
                        <select id="bookType" class="form-control">
                            <option value="">请选择图书类型</option>
                            <option>文学类</option>
                            <option>哲学类</option>
                            <option>数学类</option>
                        </select>
                    </div>
                </div>
                <div class="form-group col-sm-6  div1">
                    <label for="bookWriter" class="col-sm-8 control-label">图书作者:</label>
                    <div class="col-sm-4 ">
                        <input type="text" id="bookWriter" class="form-control">
                    </div>
                </div>
                <div class="form-group col-sm-6 div1">
                    <label for="bookPress" class="col-sm-4 control-label">出版社:</label>
                    <div class="col-sm-4">
                        <input type="text" id="bookPress" class="form-control">
                    </div>
                </div>
                <div class="form-group col-sm-6 div1">
                    <label for="bookMoney" class="col-sm-8 control-label">图书价格:</label>
                    <div class="col-sm-4 ">
                        <input type="number" id="bookMoney" class="form-control">
                    </div>
                </div>
                <div class="form-group col-sm-6 div1">
                    <label for="bookRepertory" class="col-sm-4 control-label">库存:</label>
                    <div class="col-sm-4">
                        <input type="number" id="bookRepertory" class="form-control">
                    </div>
                </div>
                <div class="form-group col-sm-12 div1 ">
                    <label for="bookDescribe" class="col-sm-4 control-label">图书描述:</label>
                    <div class="col-sm-6">
                        <textarea id="bookDescribe" class="form-control"></textarea>
                    </div>
                </div>
                <br><br>
                <div class="form-group col-sm-24 text-center">
                    <input type="button" value="提交" id="btnBook" class="btn btn-success" style="margin-right: 100px">
                    <input type="reset" class="btn">
                </div>
            </form>
        </div>

    </div>
</div>
</body>
</html>